package com.back.controller;

import com.back.annotation.AuthCheck;
import com.back.common.BaseResponse;
import com.back.common.DeleteRequest;
import com.back.common.ResultUtils;
import com.back.constant.UserConstant;
import com.back.exception.BusinessException;
import com.back.exception.ErrorCode;
import com.back.exception.ThrowUtils;
import com.back.model.entity.Weapon;
import com.back.service.WeaponService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 武器接口
 */
@RestController
@RequestMapping("/weapon")
@Slf4j
public class WeaponController {

    @Resource
    private WeaponService weaponService;

    // region 增删改查

    /**
     * 创建武器（仅管理员）
     *
     * @param weapon 武器信息
     * @return 武器ID
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Integer> addWeapon(@RequestBody Weapon weapon) {
        ThrowUtils.throwIf(weapon == null, ErrorCode.PARAMS_ERROR);
        
        boolean result = weaponService.createWeapon(weapon);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        
        return ResultUtils.success(weapon.getWeaponId());
    }

    /**
     * 删除武器（仅管理员）
     *
     * @param deleteRequest 删除请求
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteWeapon(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        Integer weaponId = Math.toIntExact(deleteRequest.getId());
        boolean result = weaponService.deleteWeapon(weaponId);
        
        return ResultUtils.success(result);
    }

    /**
     * 更新武器（仅管理员）
     *
     * @param weapon 武器信息
     * @return 是否更新成功
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateWeapon(@RequestBody Weapon weapon) {
        if (weapon == null || weapon.getWeaponId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        boolean result = weaponService.updateWeapon(weapon);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取武器（封装类）
     *
     * @param weaponId 武器ID
     * @return 武器信息
     */
    @GetMapping("/get")
    public BaseResponse<Weapon> getWeaponById(@RequestParam("weaponId") Integer weaponId) {
        ThrowUtils.throwIf(weaponId == null || weaponId <= 0, ErrorCode.PARAMS_ERROR);
        
        Weapon weapon = weaponService.getWeaponById(weaponId);
        return ResultUtils.success(weapon);
    }

    /**
     * 分页获取武器列表（仅管理员）
     *
     * @param current 当前页
     * @param pageSize 页面大小
     * @param weaponName 武器名称
     * @param heroId 英雄ID
     * @return 武器分页列表
     */
    @GetMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<IPage<Weapon>> listWeaponByPage(
            @RequestParam(value = "current", defaultValue = "1") long current,
            @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
            @RequestParam(value = "weaponName", required = false) String weaponName,
            @RequestParam(value = "heroId", required = false) Integer heroId) {
        
        Page<Weapon> page = new Page<>(current, pageSize);
        IPage<Weapon> weaponPage = weaponService.listWeaponByPage(page, weaponName, heroId);
        
        return ResultUtils.success(weaponPage);
    }

    /**
     * 获取所有武器列表（用于前端展示）
     *
     * @return 武器列表
     */
    @GetMapping("/list/all")
    public BaseResponse<List<Weapon>> getAllWeapons() {
        List<Weapon> weaponList = weaponService.getAllWeapons();
        return ResultUtils.success(weaponList);
    }

    /**
     * 根据英雄ID获取武器列表
     *
     * @param heroId 英雄ID
     * @return 武器列表
     */
    @GetMapping("/list/hero/{heroId}")
    public BaseResponse<List<Weapon>> getWeaponsByHeroId(@PathVariable Integer heroId) {
        ThrowUtils.throwIf(heroId == null || heroId <= 0, ErrorCode.PARAMS_ERROR, "英雄ID不能为空");
        
        List<Weapon> weaponList = weaponService.getWeaponsByHeroId(heroId);
        return ResultUtils.success(weaponList);
    }

    // endregion
}